package com.bsoft.gol.hcs.specialist.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import com.bsoft.gol.hcs.specialist.bean.vo.QuestionVo;
import com.bsoft.gol.hcs.specialist.entity.FollowUpRecordsExtendEntity;
import com.bsoft.gol.hcs.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class FollowUpRecordsExtendDAO extends HibernateSupportDelegateDAO<FollowUpRecordsExtendEntity> {


    @DAOMethod(sql =" from FollowUpRecordsExtendEntity where recordId=:recordId" )
    public abstract List<FollowUpRecordsExtendEntity> findFollowUpRecordsExtendEntityByRecordId(@DAOParam("recordId")Long recordId);


    public  List<QuestionVo> queryExtend(String id){

        HibernateStatelessResultAction<List<QuestionVo>> action = new AbstractHibernateStatelessResultAction<List<QuestionVo>>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuilder sql = new StringBuilder();
                sql.append(" select form_record_id questionId,form_name questionName from follow_up_records_extend where 1=1 ");
                if (StringUtils.isNotEmpty(id)){
                    sql.append(" and record_id= :id");
                }
                Query sqlQuery =ss.createSQLQuery(sql.toString());
                if (StringUtils.isNotEmpty(id)){
                    sqlQuery.setParameter("id",id);
                }
                sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map> dbList = sqlQuery.list();
                ArrayList<QuestionVo> list = new ArrayList<>();
                if (CommonUtils.isNotEmpty(dbList)){
                   list.add(ConversionUtils.convert(dbList.get(0), QuestionVo.class));
                }
                setResult(list);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();

    }



}
